<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      const inventors = [
        {
          first: "Albert",
          last: "Einstein",
          year: 1879,
          passed: 1955,
        },
        {
          first: "Isaac",
          last: "Newton",
          year: 1643,
          passed: 1727,
        },
        {
          first: "Galileo",
          last: "Galilei",
          year: 1564,
          passed: 1642,
        },
        {
          first: "Marie",
          last: "Curie",
          year: 1867,
          passed: 1934,
        },
        {
          first: "Johannes",
          last: "Kepler",
          year: 1571,
          passed: 1630,
        },
        {
          first: "Nicolaus",
          last: "Copernicus",
          year: 1473,
          passed: 1543,
        },
        {
          first: "Max",
          last: "Planck",
          year: 1858,
          passed: 1947,
        },
        {
          first: "Katherine",
          last: "Blodgett",
          year: 1898,
          passed: 1979,
        },
        {
          first: "Ada",
          last: "Lovelace",
          year: 1815,
          passed: 1852,
        },
        {
          first: "Sarah E.",
          last: "Goode",
          year: 1855,
          passed: 1905,
        },
        {
          first: "Lise",
          last: "Meitner",
          year: 1878,
          passed: 1968,
        },
        {
          first: "Hanna",
          last: "Hammarström",
          year: 1829,
          passed: 1909,
        },
      ];

      const people = [
        "Beck, Glenn",
        "Becker, Carl",
        "Beckett, Samuel",
        "Beddoes, Mick",
        "Beecher, Henry",
        "Beethoven, Ludwig",
        "Begin, Menachem",
        "Belloc, Hilaire",
        "Bellow, Saul",
        "Benchley, Robert",
        "Benenson, Peter",
        "Ben-Gurion, David",
        "Benjamin, Walter",
        "Benn, Tony",
        "Bennington, Chester",
        "Benson, Leana",
        "Bent, Silas",
        "Bentsen, Lloyd",
        "Berger, Ric",
        "Bergman, Ingmar",
        "Berio, Luciano",
        "Berle, Milton",
        "Berlin, Irving",
        "Berne, Eric",
        "Bernhard, Sandra",
        "Berra, Yogi",
        "Berry, Halle",
        "Berry, Wendell",
        "Bethea, Erin",
        "Bevan, Aneurin",
        "Bevel, Ken",
        "Biden, Joseph",
        "Bierce, Ambrose",
        "Biko, Steve",
        "Billings, Josh",
        "Biondo, Frank",
        "Birrell, Augustine",
        "Black Elk",
        "Blair, Robert",
        "Blair, Tony",
        "Blake, William",
      ];

      const data = [
        "car",
        "car",
        "truck",
        "truck",
        "bike",
        "walk",
        "car",
        "van",
        "bike",
        "walk",
        "car",
        "van",
        "car",
        "truck",
        "pogostick",
      ];
      //筛选 16 世纪出生的发明家
      // const fifteen = inventors.filter(inventor=>{
      //   // if (inventor.year >= 1500)
      //   // return inventor.year <=1600
      //   return inventor.year >=1500 && inventor.year <= 1600
      // })
      const fifteen = inventors.filter(inventor => (inventor.year >=1500 && inventor.year <= 1600))

      //展示他们的姓和名
      const fullName = inventors.map(inventor => (`${inventor.first} ${inventor.last}`))

      //把他们按照年龄从大到小进行排序
      // const ordered = inventors.sort((a,b) => {
      //   if (a.year > b.year) {
      //     return 1
      //   } else {
      //     return -1
      //   }
      // })
      const ordered = inventors.sort((a, b) => a.year > b.year ? 1 : -1)

      //计算所有的发明家加起来一共活了多少岁
      const totalY = inventors.reduce((total, inventor) => {
        return total + (inventor.passed - inventor.year)
      }, 0)

      //按照他们活了多久来进行排序
      const oldest = inventors.sort((a, b) => {
        const lastInventor = a.passed - a.year
        const nextInventor = b.passed - b.year
        return lastInventor > nextInventor ? 1 : -1
      })

      //map、filter结合使用筛选出网页中含有CSS标题的数据名称
      const category = document.querySelectorAll('.list h2 a')
      const links = Array.from(category)
      const CSS_BOOK = links
        .map(link => link.textContent)
        .filter(streetName => streetName.includes('CSS'))

      //按照姓氏来对发明家进行排序
      const inventorA = people.sort((a, b) =>{
        const {aLast, aFirst} = a.split(', ')
        const {bLast, bFirst} = b.split(', ')
        return aLast > bLast ? 1 : -1
      })

      //统计给出数组中各个物品的数量
      const newData = data.reduce((obj, item) => {
        if(!obj[item]){
          obj[item] = 0
        }
        obj[item]++
        return obj
      }, {})
      console.table(newData)
      console.log(newData);
    </script>
  </body>
</html>
